<?php
	// *** Include the class
	include("resize-class.php");
	
	Class ImageUploader
		{
			// *** Class variables
			private $errors = array();
			private $data;
			private $success;
			private $db_info;
			private $ref_id;
			private $file_name;
			private $org_file_name;
			private $fp_image = "upload_image/";
			private $fp_user_image = "upload_user_image/";
			private $fp_user_image_thmp = "upload_user_image/thumps/";
			private $fp_pdf = "upload_pdf/";
			private $fp_attachment = "attachments/";
			private $fp_image_thump = "upload_image/thumps/";
			private $fp_image_crop = "upload_image/crop";
			
			function __construct($fileName, $id)
			{
				$this -> file_name = uniqid("auto").'.'.self::file_extension($fileName);
				$this -> org_file_name = $fileName;
				$this -> ref_id = $id;
			}
			
			private function file_extension($fileName)
			{
			    return end(explode(".", $fileName));
			}
			
			public function upload($file_temp, $type)
			{
				switch ($type) {
					// *** In case of vehicle images and pdf
					case 'upload':
						// *** if filepdf
						// *** if filepdf
						if (self::file_extension($this -> org_file_name) == "pdf")
	   					{
							writeFileOnDisk($file_temp, $this -> fp_pdf);
							
							$value = self::insertVehicleIntoDatabase($this -> fp_pdf.$org_file, $this -> fp_pdf.'thumb.png', 
							$this -> fp_pdf.'crop.png');
						}else
						{
							self::writeFileOnDisk($file_temp, $this ->fp_image);
							
							$value = self::insertVehicleIntoDatabase($this -> fp_image.$org_file, $this -> fp_image.'thumps_'.$this ->file_name, 
							$this -> fp_image.'crop_'.$this ->file_name);
							// *** 1) Initialise / load image
							$resizeObj = new resize($this -> fp_image.$org_file);
							// *** 2) Resize image (options: exact, portrait, landscape, auto, crop)
							$resizeObj -> resizeImage(200, 200, 'crop');
							// *** 3) Save image
							$resizeObj -> saveImage($this -> fp_image.'crop_'.$this ->file_name, 100);
							// *** 1) Initialise / load image
							$resizeObj = new resize($this -> fp_image.$org_file);
							// *** 2) Resize image (options: exact, portrait, landscape, auto, crop)
							$resizeObj -> resizeImage(50, 40, 'crop');
							// *** 3) Save image
							$resizeObj -> saveImage($this -> fp_image.'thumps_'.$this ->file_name, 100);
						}
						
						return $value;
						
						break;
					
					case 'adminImageUpload':
						
						self::writeFileOnDisk($file_temp, $this -> fp_user_image);
						// *** 1) Initialise / load image
						$resizeObj = new resize('sample.jpg');
						// *** 2) Resize image (options: exact, portrait, landscape, auto, crop)
						$resizeObj -> resizeImage(50, 40, 'crop');
						// *** 3) Save image
						$resizeObj -> saveImage('sample-resized.jpg', 100);
						
						
						$my_insert = "UPDATE administrator SET `image_url` ='". $this -> fp_user_image.$this -> file_name."'
						,`image_thump`='".$this -> $fp_user_image_thmp.'thumps_'.$this -> file_name."' WHERE admin_id=".$this -> ref_id;
						$my_insert_1 = mysql_query($my_insert) or die("Query ERROR");
						return $my_insert_1;
						
						break;
						
					case 'uploadAttachment':
						
						writeFileOnDisk($file_temp, $this -> $fp_attachment);
						
						$myDate = date('Y-m-d H:i:s');
					    $my_insert = "INSERT INTO attachments ( `org_file_name` , `server_file_name`, `date` ) VALUES 
					    ('".$this -> file_name. "',".$this -> org_file_name.",'" .$myDate. "')";
					    $my_insert = mysql_query($my_insert) or die("Query ERROR");
					    $result_sel = mysql_insert_id();
								   
					    $result = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
						$result .= "<results>\n";
						$result .= "<success>".$success."</success>";
						$result .= "<orgName>".$this -> org_file_name."</orgName>";
						$result .= "<serverName>".$this -> file_name."</serverName>";
						$result .= "<attachID>".$this -> ref_id."</attachID>";
						for($i=0;$i<count($this -> errors);$i++)
						{
						    $result .= "<error>".$this -> errors[$i]."</error>";
						}
						$result .=    "</results>";
						
					    return $result;
								
						break;
					
					case 'senchaUpload':
						
						// *** if filepdf
						if (file_extension($this -> org_file_name) == "pdf")
	   					{
							self::writeFileOnDisk($file_temp, $this -> fp_pdf);
							
							$value = self::insertVehicleIntoDatabase($this -> fp_pdf.$org_file, $this -> fp_pdf.'thumb.png', 
							$this -> fp_pdf.'crop.png');
						}else
						{
							self::writeFileOnDisk($file_temp, $this ->fp_image);
							
							$value = self::insertVehicleIntoDatabase($this -> fp_image.$org_file, $this -> fp_image.'thumps_'.$this ->file_name, 
							$this -> fp_image.'crop_'.$this ->file_name);
							// *** 1) Initialise / load image
							$resizeObj = new resize($this -> fp_image.$org_file);
							// *** 2) Resize image (options: exact, portrait, landscape, auto, crop)
							$resizeObj -> resizeImage(200, 200, 'crop');
							// *** 3) Save image
							$resizeObj -> saveImage($this -> fp_image.'crop_'.$this ->file_name, 100);
							// *** 1) Initialise / load image
							$resizeObj = new resize($this -> fp_image.$org_file);
							// *** 2) Resize image (options: exact, portrait, landscape, auto, crop)
							$resizeObj -> resizeImage(50, 40, 'crop');
							// *** 3) Save image
							$resizeObj -> saveImage($this -> fp_image.'thumps_'.$this ->file_name, 100);
						}

						if($this -> success == 'true')
						{
							echo '{success:true}';
						}
						
						break;
						
					default:
						$this -> success = "false";
    					array_push($this -> errors,"No action was requested.");
						break;
				}
   
   			}

			private function writeFileOnDisk($file_temp, $root)
			{
				$filestatus = move_uploaded_file($file_temp, $root.$this -> file_name);
				if(!file_exists($root.$this -> file_name)){
		            if(!$filestatus){
		                 $this -> success = "false";
		                 array_push($this -> errors, "Upload failed. Please try again.");
		            }else {
		                $this -> success = "true";
		            }
					
		        }
				else {
		            $this -> success = "false";
		            array_push($this -> errors,"File already exists on server.");
		        }
			}
			
			private function insertVehicleIntoDatabase($org_file, $thumb_file, $crop_file)
			{
			    $my_insert = "INSERT INTO vehicle_image (  `image_url` , `vehicle_id`, `thump_url`, `crop_url` ) VALUES 
			    ('".$org_file. "',".$this -> ref_id.",'" .$thumb_file. "','" .$crop_file. "')";
			    $my_insert = mysql_query($my_insert) or die("Query ERROR");
			    $result_sel = mysql_insert_id();
				
			    /*$up_type = $_REQUEST['upload_type'];
			    if($my_insert && $up_type='seller')
			    {
			        $my_select = "SELECT email,first_name FROM vehicle WHERE vehicle_id=".$vehID;
			        $my_select = mysql_query($my_select) or die("Query ERROR");
			        $row = mysql_fetch_object($my_select);
			    
			        $_instance = new VehicleService();
			        $messages = 'Seller added new picture';
			        $colName ='update_date';
			        $reply ='New picture uploaded';
			        $vehicleVO = $_instance->updateMessageVehicleID($messages, $colName, $reply, false, NULL);
			    }*/
			   
			    $result = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
			    $result .= "<results>\n";
			    $result .= "<success>".$success."</success>";
			    $result .= "<imagename>".$org_file."</imagename>";
			    $result .= "<imageid>".$result_sel."</imageid>";
			    $result .= "<imageUrl>".$org_file."</imageUrl>";
			    $result .= "<thumpUrl>".$thumb_file."</thumpUrl>";
			    $result .= "<vehicleId>".$this -> ref_id."</vehicleId>";
				
			    for($i=0;$i<count($this -> errors);$i++)
			    {
			        $result .= "<error>".$this -> errors[$i]."</error>";
			    }
			    $result .=    "</results>";
			    
			    return $result;
			}

		}
?>
